package com.aos.erp.manager.rest.sys;

import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.constant.GlobalConstant;
import com.aos.erp.manager.enums.IsDel;
import com.aos.erp.manager.model.sys.Department;
import com.aos.erp.manager.model.sys.vo.MenuTree;
import com.aos.erp.manager.service.sys.DepartmentService;
import com.aos.erp.manager.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: erp
 * @description: 部门组织controller
 * @author: tanzhiyu
 * @create: 2018-11-01 19:27
 **/
@Api(value = "部门组织api", tags = {"部门组织API"})
@RestController("RESTDepartmentController")
@RequestMapping("/api/department")
public class DepartmentController {
    @Resource
    private DepartmentService departmentService;
    @Resource
    GlobalConstant globalConstant;

    @ApiOperation(value = "新增部门组织---部门")
    @PostMapping("/addDepDepartment")
    public ResultEntity addDepDepartment(@RequestBody Map<String, Object> params) throws Exception {
        if (params.get("depCode") == null || params.get("name") == null) {
            return ResultEntity.failed("部门code或名称不能为空", ResultEntity.CODE_NULL);
        }
        String code = departmentService.addBeanDepartment(params.get("depCode").toString(), params.get("name").toString(), globalConstant.getAPP_CODE_AOS());
        return ResultEntity.success(code, "新增部门成功");
    }

    @ApiOperation(value = "删除部门组织")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depId", value = "部门ID", required = true, dataType = "String", paramType = "Query")
    })
    @GetMapping("/delDep")
    public ResultEntity delDep(String depId) throws Exception {
        if (StringUtils.isBlank(depId)) {
            return ResultEntity.failed("部门ID不能为空", ResultEntity.CODE_NULL);
        }

        int i = departmentService.isDel(depId, globalConstant.getAPP_CODE_AOS());
        if (i > 0) {
            return ResultEntity.success("删除成功");
        } else {
            return ResultEntity.success("删除失败");
        }
    }

    @ApiOperation(value = "修改部门组织")
    @PostMapping("/updateDepartment")
    public ResultEntity updateDepartment(@RequestBody Map<String, Object> params) throws Exception {
        if (params.get("depId") == null || params.get("name") == null) {
            return ResultEntity.failed("部门ID或部门名称不能为空", ResultEntity.CODE_NULL);
        }
        int i = departmentService.updateObject(params.get("depId").toString(), params.get("name").toString(), globalConstant.getAPP_CODE_AOS());
        if (i > 0) {
            return ResultEntity.success("修改成功");
        } else {
            return ResultEntity.success("修改失败");
        }
    }


    @ApiOperation(value = "根据部门code获取部门信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "depCode", value = "部门code", required = true, dataType = "String", paramType = "Query")
    })
    @PostMapping("/getDepartmentCode")
    public ResultEntity getDepartmentCode(String depCode) throws Exception {
        if (StringUtils.isBlank(depCode)) {
            return ResultEntity.failed("部门code不能为空", ResultEntity.CODE_NULL);
        }
        Department department = departmentService.findByDepCode(depCode);
        return ResultEntity.success(department);
    }

    @ApiOperation(value = "根据部门code获取部门组织树")
    @GetMapping("/getDepTree")
    public ResultEntity getDepTree(String currentUserId) throws Exception {
        //TODO
        String depCode = UserUtil.getUserDepCode(currentUserId);
        Map<String, Object> params = new HashMap<>();
        params.put("depCode", depCode);
        params.put("isDel", IsDel.normal.getValue().toString());
        List<MenuTree> menuTrees = departmentService.selectTree(params);
        return ResultEntity.success(menuTrees);
    }

    @ApiOperation(value = "同步根据当前用户机构code查角色信息")
    @PostMapping("/queryByOrganizationCode")
    public ResultEntity queryByOrganizationCode(String currentUserId) throws Exception {
        //TODO
        Object o = departmentService.queryByOrganizationCode(currentUserId);
        return ResultEntity.success(o);
    }

    @ApiOperation(value = "同步根据机构code查角色信息")
    @PostMapping("/queryRoleByOrganizationCode")
    public ResultEntity queryRoleByOrganizationCode(String organizationCode) throws Exception {
        //TODO
        Object o = departmentService.queryRoleByOrganizationCode(organizationCode);
        return ResultEntity.success(o);
    }
}
